package com.atguigu.gulimall.order.listener;

import com.atguigu.gulimall.order.entity.OrderEntity;
import com.atguigu.gulimall.order.service.OrderService;
import com.rabbitmq.client.Channel;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;

/**
 * @author: haitao
 * @email: haitaoss@aliyun.com
 * @date: 2020/7/21 18:46:47
 * @Description
 */

@Service
@RabbitListener(queues = {"order.release.order.queue"})
public class OrderCloseListener {
    @Autowired
    OrderService orderService;

    @RabbitHandler()
    public void listener(Message message, OrderEntity entity, Channel channel) throws IOException {
        System.out.println("收到过期的订单信息，准备关闭订单" + entity.toString());
        try {
            // 手动关闭支付宝订单
            orderService.closeOrder(entity);
            // 手动签收消息
            channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
        } catch (Exception e) {
            // 消息消费失败，重新入队
            channel.basicReject(message.getMessageProperties().getDeliveryTag(), true);
        }

    }
}
